package com.xsqf.jt808.netty.handler;

import com.xsqf.jt808.common.util.BeanUtil;
import com.xsqf.jt808.common.vo.req.terminal.RegisterMsg;
import com.xsqf.jt808.common.vo.resp.RegisterResp;
import com.xsqf.jt808.entity.VehicleRegisterInfo;
import com.xsqf.jt808.service.IVehicleAuthInfoService;
import com.xsqf.jt808.service.IVehicleRegisterInfoService;
import com.xsqf.jt808.common.vo.resp.CommonResp;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.UUID;

/**
 * TerminalComResp
 * 注册消息->RegisterResp
 *
 * @author
 * @date 2020/5/15 10:35
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class RegisterMsgHandler extends BaseHandler<RegisterMsg> {

//    @Autowired
//    AuthInfoDao authInfoDao;
//    @Autowired
//    RegisterInfoDao registerInfoDao;

    @Autowired
    IVehicleAuthInfoService authInfoService;
    @Autowired
    IVehicleRegisterInfoService registerInfoService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RegisterMsg msg) {
        log.info("注册：" + msg.toString());
        //默认鉴权成功
        RegisterResp resp = success(msg, getSerialNumber(ctx.channel()));
        write(ctx, resp);
    }

    public RegisterResp success(RegisterMsg msg, short flowId) {
        RegisterResp resp = new RegisterResp();
        resp.getHeader().setTerminalPhone(msg.getHeader().getTerminalPhone());
        resp.getHeader().setFlowId(flowId);
        resp.setReplyFlowId(msg.getHeader().getFlowId());
        String authCode = UUID.randomUUID().toString();
        RegisterMsg registerMsg = null;
        try {
            registerMsg = (RegisterMsg) BeanUtil.beanAttributeValueTrim(msg);
        } catch (Exception e) {
            resp.setResult(RegisterResp.VEHICLE_NOT_IN_DB);
            return resp;
        }

        // 查询该终端/车辆是否存在该信息
//        VehicleAuthInfo authInfo = authInfoService.findAllByParameter(registerMsg.getLicensePlate(), registerMsg.getHeader().getTerminalPhone());
        Map<Object, Object> authMap = redisTemplate.opsForHash().entries("AUTH:" + registerMsg.getHeader().getTerminalPhone());

//        if (authInfo != null) {
        if (authMap.size() > 0) {
            resp.setResult(CommonResp.SUCCESS);

            //保存或更新车辆注册信息
            VehicleRegisterInfo registerInfo = registerInfoService.findByLicensePlate(registerMsg.getLicensePlate());
            if (registerInfo != null) {
                BeanUtils.copyProperties(registerMsg, registerInfo);
                registerInfo.setTerminalId(registerMsg.getHeader().getTerminalPhone());
                registerInfo.setProvinceId(registerMsg.getProvinceId().toString());
                registerInfo.setCityId(registerMsg.getCityId().toString());
                registerInfo.setLicensePlateColor(registerMsg.getLicensePlateColor().toString());
                registerInfoService.save(registerInfo);
                resp.setAuthCode(registerInfo.getAuthCode());

            } else {
                registerInfo = new VehicleRegisterInfo();
                BeanUtils.copyProperties(registerMsg, registerInfo);
                registerInfo.setTerminalId(registerMsg.getHeader().getTerminalPhone());
                registerInfo.setProvinceId(registerMsg.getProvinceId().toString());
                registerInfo.setCityId(registerMsg.getCityId().toString());
                registerInfo.setLicensePlateColor(registerMsg.getLicensePlateColor().toString());
                registerInfo.setAuthCode(authCode);
                registerInfo.setDeleted("0");
                registerInfoService.save(registerInfo);
                resp.setAuthCode(authCode);

            }
        } else {
            resp.setResult(RegisterResp.VEHICLE_NOT_IN_DB);
        }
        return resp;
    }
}
